package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.user.TemplateException;
import com.ruoyi.common.utils.file.FastDFSUtil;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.domain.SysCertificate;
import com.ruoyi.system.domain.SysCertificateTeacher;
import com.ruoyi.system.domain.Work;
import com.ruoyi.system.service.ISysCertificateService;
import com.ruoyi.system.service.ISysCertificateTeacherService;
import com.ruoyi.system.service.WorkService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 柳
 * @Date 2021/5/9
 * @Description TODO
 */
@RestController
@RequestMapping("/system/certificateteacher")
public class SysCertificateTeacherController {
    @Autowired
    private FastDFSUtil fastDFSUtil;

    @Autowired
    private ISysCertificateTeacherService sysCertificateTeacherService;

    @Autowired
    private WorkService workService;


    /**
     * 删除证书信息
     */
    @DeleteMapping("/deleteSysCertificateTeacher/{certificateid}")
    @ApiOperation("删除证书信息")
    public AjaxResult deleteSysCertificateTeacher(@PathVariable Integer certificateid) {
        sysCertificateTeacherService.deleteSysCertificateTeacher(certificateid);
        return AjaxResult.success();

    }

    /**
     * 修改证书信息
     */
    @PutMapping("/updateSysCertificateTeacher")
    @ApiOperation("修改证书信息")
    public AjaxResult updateSysCertificateTeacher(SysCertificateTeacher sysCertificateTeacher) {
        if(sysCertificateTeacher==null){
            return AjaxResult.error("数据异常,操作失败");
        }
//        //查询是否有重复证书信息
//        Integer count = sysCertificateTeacherService.queryCertificateTeacherIsNull(sysCertificateTeacher);
//        if(count>0){
//            return AjaxResult.error("请勿重复颁发！！！");
//        }
        //删除原先的pdf图片
        FileUtils.deleteFile(System.getProperty("user.dir")+"\\ruoyi-admin\\src\\main\\resources\\zhengshu\\pdfimage.png");
        //sysCertificateService.querySysCertificatesById(certificateid);
        sysCertificateTeacherService.updateSysCertificateTeacher(sysCertificateTeacher);
        return AjaxResult.success();

    }



    @GetMapping("/querySysCertificateTeacherById/{certificateid}")
    @ApiOperation("根据教师证书id查询教师证书信息")
    public AjaxResult querySysCertificateTeacherById(@PathVariable Integer certificateid) {
        SysCertificateTeacher sysCertificateTeacher = sysCertificateTeacherService.querySysCertificateTeacherById(certificateid);
        return AjaxResult.success(sysCertificateTeacher);
    }

    @GetMapping("/querySysCertificateTeachers")
    @ApiOperation("查询所有老师证书信息包含综合查询")
    public AjaxResult querySysCertificateTeachers(SysCertificateTeacher sysCertificateTeacher) {
        if(sysCertificateTeacher.getCurrentPage()==null || sysCertificateTeacher.getPageSize()==null){
            return AjaxResult.error();
        }
        PageHelper.startPage(sysCertificateTeacher.getCurrentPage(),sysCertificateTeacher.getPageSize());
        List<SysCertificateTeacher> sysCertificateTeacherList=sysCertificateTeacherService.querySysCertificateTeachers(sysCertificateTeacher);
        PageInfo<SysCertificateTeacher>  pageInfo=new PageInfo<>(sysCertificateTeacherList);

        return AjaxResult.success(pageInfo);
    }




    @PostMapping("/saveSysCertificateTeacher")
    @ApiOperation("添加教师证书信息(颁发证书)")
    public AjaxResult saveSysCertificateTeacher(SysCertificateTeacher sysCertificateTeacher){
        if(sysCertificateTeacher==null){
            return AjaxResult.error("数据异常,操作失败");
        }
        //查询是否有重复证书信息
        Integer count = sysCertificateTeacherService.queryCertificateTeacherIsNull(sysCertificateTeacher);
        if(count>0){
            return AjaxResult.error("请勿重复颁发！！！");
        }
        //删除原先的pdf图片
        FileUtils.deleteFile(System.getProperty("user.dir")+"\\ruoyi-admin\\src\\main\\resources\\zhengshu\\pdfimage.png");

        try {
            sysCertificateTeacherService.saveSysCertificateTeacher(sysCertificateTeacher);
        } catch (TemplateException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getCode(), e.getMessage());
        }
        return AjaxResult.success();
    }

}
